package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.ParkBlock;
import com.qingxin.park.entity.SysUserBlockRel;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.service.SysUserService;
import com.qingxin.sys.utils.StringUtils;


/**
 * 用户-园区地块
 * @author Administrator
 *
 */
@Service
public class SysUserBlockRelService extends BaseService<SysUserBlockRel, String> {
	@Autowired
	private SysUserCompRelService sysUserCompRelService;
	@Autowired
	private SysUserParkRelService sysUserParkRelService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysGroupService sysGroupService;
	
	
	/**
	 * 添加用户-地块关联
	 * @param userid
	 * @param blockid
	 * @param loginUser
	 * @return
	 */
	public SysError addUserBlockRel(String userid, String blockid, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(blockid)){
			return null;
		}
		Object id = getByHql("select id from SysUserBlockRel where sysUser.userid=? and parkBlock.blockid=?", userid, blockid);
		if(id != null){
			return null;
		}
		
		SysUserBlockRel rel = new SysUserBlockRel(new ParkBlock(blockid), new SysUser(userid), loginUser.getLogin_name(), new Date());
		this.save(rel);
		
		return null;
	}
	
	
	/**
	 * 设置用户-地块查看权限
	 * @param userid
	 * @param blockids
	 * @param loginUser
	 * @return
	 */
	public SysError saveUserBlockRels(String userid, String[] blockids, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) ){
			return null;
		}
		//删除现有关联
		deleteByUserid(userid);
		
		if(blockids==null || blockids.length==0){
			return null;
		}
		
		Date now = new Date();
		String createBy = loginUser.getLogin_name();
		
		for(String blockid : blockids){
			SysUserBlockRel rel = new SysUserBlockRel(new ParkBlock(blockid), new SysUser(userid), createBy, now);
			this.save(rel);
		}
		
		return null;
	}
	
	
	/**
	 * 获取指定用户关联的地块id集合
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Set<String> getUserBlockids(String userid){
		Set<String> set = new HashSet<String>();
		if(StringUtils.isEmpty(userid) ){			
			return set;
		}
		
		List<String> list = listByHql("select parkBlock.blockid from SysUserBlockRel where sysUser.userid=?", userid);
		if(list != null){
			set.addAll(list);
		}
		
		return set;
	}
	
	
	
	/**
	 * 删除用户-地块关联
	 * @param userid
	 * @return
	 */
	public Object deleteByUserid(String userid){
		deleteByHql("delete from SysUserBlockRel where sysUser.userid=?", userid);
		return null;
	}
	
	
	
//	/**
//	 * 设置用户-园区-地块关联关系（数据权限）的数据
//	 * @param userid
//	 * @param loginUser
//	 * @return
//	 */
//	@SuppressWarnings("unchecked")
//	public Object getSetParkData(String userid, LoginUser loginUser){
//		List<Map<String, String>> list = new ArrayList<>();
//		Set<String> rels = new HashSet<>();
//		rels.addAll(getUserBlockids(userid));
//		rels.addAll(sysUserParkRelService.getUserParkids(userid));
//		
//		if(SystemCfg.isDataAdminUser(loginUser)){
//			List<FarmPark> parks = listByHql("select new com.qingxin.park.entity.FarmPark(p.parkid, p.parkName)" +
//					" from FarmPark p where p.parkStatus='1' order by p.parkName asc");
//			if(parks != null){
//				for(FarmPark park : parks){
//					Map<String, String> node = new HashMap<String, String>();
//					node.put("id", park.getParkid());
//					node.put("name", park.getParkName());
//					node.put("pid", "0");
//					node.put("hasRights", ""+rels.contains(park.getParkid()));
//					list.add(node);
//				}
//			}
//			List<ParkBlock> blocks = listByHql("select new com.qingxin.park.entity.ParkBlock(b.blockid, b.blockName, b.farmPark.parkid)" +
//					" from ParkBlock b order by b.blockName asc");
//			if(blocks != null){
//				for(ParkBlock block : blocks){
//					Map<String, String> node = new HashMap<String, String>();
//					node.put("id", block.getBlockid());
//					node.put("name", block.getBlockName());
//					node.put("pid", block.getFarmPark().getParkid());
//					node.put("hasRights", ""+rels.contains(block.getBlockid()));
//					list.add(node);
//				}
//			}
//		}else {
//			List<FarmPark> parks = listByHql("select new com.qingxin.park.entity.FarmPark(p.parkid, p.parkName)" +
//					" from FarmPark p where p.parkStatus='1'" +
//					" and p.parkid in (select distinct v.farmPark.parkid from SysUserParkRel v where v.sysUser.userid=?)" +
//					" order by p.parkName asc", loginUser.getUserid());
//			if(parks != null){
//				for(FarmPark park : parks){
//					Map<String, String> node = new HashMap<String, String>();
//					node.put("id", park.getParkid());
//					node.put("name", park.getParkName());
//					node.put("pid", "0");
//					node.put("hasRights", ""+rels.contains(park.getParkid()));
//					list.add(node);
//				}
//			}
//			List<ParkBlock> blocks = listByHql("select new com.qingxin.park.entity.ParkBlock(b.blockid, b.blockName, b.farmPark.parkid)" +
//					" from ParkBlock b" +
//					" where b.blockid in (select distinct v.parkBlock.blockid from SysUserBlockRel v where v.sysUser.userid=?)" +
//					" order by b.blockName asc", loginUser.getUserid());
//			if(blocks != null){
//				for(ParkBlock block : blocks){
//					Map<String, String> node = new HashMap<String, String>();
//					node.put("id", block.getBlockid());
//					node.put("name", block.getBlockName());
//					node.put("pid", block.getFarmPark().getParkid());
//					node.put("hasRights", ""+rels.contains(block.getBlockid()));
//					list.add(node);
//				}
//			}
//		}
//		
//		return list;
//	}
	
	
	
	
	/**
	 * 设置用户-园区-地块关联关系（数据权限）的数据
	 * @param userid
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object getSetParkData(String userid, LoginUser loginUser2){
		List<Map<String, String>> list = new ArrayList<>();
		Set<String> rels = new HashSet<>();
		rels.addAll(getUserBlockids(userid));
		rels.addAll(sysUserParkRelService.getUserParkids(userid));
		rels.addAll(sysUserCompRelService.getUserCompanyids(userid));
		
		SysUser user = sysUserService.get(userid);
		
		String uroupid = user.getGroupid();
		SysGroup group = sysGroupService.get(uroupid);
		if(group.getGrouptype() == 3){
			uroupid = group.getPid();
		}
		
		List<Company> companies = listByHql("select new com.qingxin.park.entity.Company(c.companyid, c.companyName)" +
				" from Company c where c.groupid like '"+uroupid+"%'" +
				" order by c.companyName asc");
		if(companies != null){
			for(Company company : companies){
				Map<String, String> node = new HashMap<String, String>();
				node.put("id", company.getCompanyid());
				node.put("name", company.getCompanyName());
				node.put("pid", "0");
				node.put("hasRights", ""+rels.contains(company.getCompanyid()));
				node.put("datatype", "comp");
				list.add(node);
			}
		}
		
		List<FarmPark> parks = listByHql("select new com.qingxin.park.entity.FarmPark(p.parkid, p.parkName, p.companyid)" +
				" from FarmPark p where p.parkStatus='1' and p.groupid like '"+user.getGroupid()+"%'" +
				" order by p.parkName asc");
		if(parks != null){
			for(FarmPark park : parks){
				Map<String, String> node = new HashMap<String, String>();
				node.put("id", park.getParkid());
				node.put("name", park.getParkName());
				node.put("pid", park.getCompanyid());
				node.put("hasRights", ""+rels.contains(park.getParkid()));
				node.put("datatype", "park");
				list.add(node);
			}
		}
		List<ParkBlock> blocks = listByHql("select new com.qingxin.park.entity.ParkBlock(b.blockid, b.blockName, b.farmPark.parkid)" +
				" from ParkBlock b,FarmPark p where b.farmPark.parkid=p.parkid and p.groupid like '"+user.getGroupid()+"%'" +
				" order by b.blockName asc");
		if(blocks != null){
			for(ParkBlock block : blocks){
				Map<String, String> node = new HashMap<String, String>();
				node.put("id", block.getBlockid());
				node.put("name", block.getBlockName());
				node.put("pid", block.getFarmPark().getParkid());
				node.put("hasRights", ""+rels.contains(block.getBlockid()));
				node.put("datatype", "block");
				list.add(node);
			}
		}
		
		return list;
	}
	
	
	
}
